package com.example.demo1.controller;

import com.example.demo1.entity.Girls;
import com.example.demo1.entity.Result;
import com.example.demo1.repository.GirlRepository;
import com.example.demo1.service.GirlService;
import com.example.demo1.until.ResultUntil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

@RestController
public class GirlController {
    @Autowired
    private GirlRepository girlRepository;
    @Autowired
    private GirlService girlService;

    /**
     * 查询所有女生列表
     */
    @RequestMapping(value = "/girl/findall")
    public List<Girls> findAll() {
        return girlRepository.findAll();
    }

    /**
     * 添加一个女生
     */
    @GetMapping(value = "/girl/addgirl")
    public Result<Girls> girlAdd(@Valid Girls girls, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResultUntil.error(1, bindingResult.getFieldError().getDefaultMessage());
        }
        girls.setCupSize(girls.getCupSize());
        girls.setMoney(girls.getMoney());
        girls.setAge(girls.getAge());


        return ResultUntil.success(girlRepository.save(girls));
    }

    /**
     * 添加两个女生
     */

    @GetMapping(value = "/girl/insertgirl")
    public void insertGirl() {
        girlService.insertTwo();
    }

    /*
    * 根据id查询女生信息
    * */
    @GetMapping(value = "/girl/findone")
    public void findOnde(@RequestParam(value = "id", required = false) Integer id) throws Exception {
        girlService.findeOne(id);
    }


}
